package com.glyxybxhtxt.util;

import com.glyxybxhtxt.dataObject.Bxd;
import com.glyxybxhtxt.service.IDictService;
import com.glyxybxhtxt.service.JdrService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author George
 * @project glyxybxxththd
 * @package com.glyxybxhtxt.util
 * @date 2021/12/6 14:45
 * @since 1.0
 */
@Component
@Slf4j
public class WeChatNotify {
    @Autowired
    private IDictService ds;
    @Autowired
    private ParseUtil pu;
    @Autowired
    private JdrService js;

    // 发送模板信息
    public String push(String openid,String map, Bxd bxd,String eid) {
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wxacc93d31c5dbd26f");
        wxStorage.setSecret("f8c9e82b590ac71eb12e96f77cf65740");
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                //要推送的用户openid
                .toUser(openid)
                //模版id
                .templateId("-1pT4MrVw4asKamh0IQMF6c22Ih74dRjxqN8fwhjoQs")
                //点击模版消息要访问的网址
                .url("http://yiban.glmc.edu.cn/bx?eid="+eid)
                .build();

        // 获取保修类别
        String bxlb=pu.paraseBxlb(bxd.getBxlb());
        //3,正式版发送模版消息，这里需要配置你的信息，替换微信公众号上创建的模板内容
        templateMessage.addData(new WxMpTemplateData("map", map, "#173177"));
        templateMessage.addData(new WxMpTemplateData("type",bxlb, "#173177"));
        templateMessage.addData(new WxMpTemplateData("text", bxd.getXxdd()+bxd.getBxnr(), "#173177"));
        templateMessage.addData(new WxMpTemplateData("time", bxd.getYysj(), "#173177"));
        templateMessage.addData(new WxMpTemplateData("name", bxd.getSbr(), "#173177"));
        templateMessage.addData(new WxMpTemplateData("phone", bxd.getSbrsj(), "#173177"));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            return "推送成功";
        } catch (Exception e) {
            log.info("推送失败：" + e.getMessage());
            e.printStackTrace();
            return "推送失败";
        }
    }

    // 检查openid是否已经绑定
    public String selOpenid(String ybid){
        String openid=js.selOpenidForYbid(ybid);
        if (openid == null){
            return null;
        }
        return openid;
    }
}
